//
//  ViewController.m
//  PlayeDemo
//
//  Created by liusha on 2019/6/20.
//  Copyright © 2019 liusha. All rights reserved.
//

#import "ViewController.h"

#import <SuperPlayer.h>
#import <CFDanmaku/CFDanmakuView.h>
#import <Masonry/Masonry.h>

#import "DSVideoShareView.h"

@interface ViewController () <SuperPlayerDelegate, CFDanmakuDelegate, DSVideoShareViewDelegate>

@property (nonatomic, strong) UIView *playerFatherView;

@property (nonatomic, strong) SuperPlayerView *playerView;


/** 弹幕视图 */
@property (nonatomic, strong) CFDanmakuView *danmakuView;
/** 弹幕数据 */
@property (nonatomic, strong) NSMutableArray *danmakuArray;

/** 弹幕开始显示时间 */
@property (nonatomic, assign) NSInteger startTime;

/** 视频已经播放的秒数 */
@property (nonatomic, assign) NSInteger playbackProgressCount;

@property (nonatomic, strong) UIView *danmakuMaskView;


@property (nonatomic, strong) DSVideoShareView *shareView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupViews];
    [self ds_updateViewConstraints];
    
}

- (void)setupViews {
    
    [self.view addSubview:self.playerFatherView];
    self.playerView.fatherView = self.playerFatherView;
    
    [self setupPlayer];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.playerView pause];
    
    __weak typeof(self) weakSelf = self;
    [self.playerView addSubview:self.shareView];
    
    [self.shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.playerView);
        make.top.equalTo(weakSelf.playerView).offset(44);
    }];
}

- (void)ds_updateViewConstraints {
    __weak typeof(self) weakSelf = self;
    [self.playerFatherView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(weakSelf.view.mas_safeAreaLayoutGuideTop);
        } else {
            make.top.mas_equalTo(20 + weakSelf.navigationController.navigationBar.bounds.size.height);
        }
        make.leading.trailing.mas_equalTo(0);
        // 这里宽高比16：9,可自定义宽高比
        make.height.mas_equalTo(weakSelf.view.mas_width).multipliedBy(9.0f/16.0f).offset(40);
    }];
}

/** 初始化播放器 */
- (void)setupPlayer {
    
    SuperPlayerModel *model = [[SuperPlayerModel alloc] init];
    model.videoURL =  @"https://www.apple.com/105/media/us/iphone-xs/2018/674b340a-40f1-4156-bbea-00f386459d3c/films/design/iphone-xs-design-tpl-cc-us-2018_960x540.mp4";
    [self.playerView playWithModel:model];
    
}

/** 初始化弹幕视图 */
- (void)setupDanmaku {
    __weak typeof(self) weakSelf = self;
    [self.playerView addSubview:self.danmakuView];
    
    [_danmakuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.playerView);
        make.bottom.equalTo(weakSelf.playerView);
        make.left.equalTo(weakSelf.playerView);
        make.right.equalTo(weakSelf.playerView);
    }];
    
    SPDefaultControlView *dv = (SPDefaultControlView *)self.playerView.controlView;
    
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    BOOL danMaStart = [[userD objectForKey:@"DSVideoDanmaStart"] boolValue];
    if (danMaStart) { dv.danmakuBtn.selected = YES; }
    
    [dv.danmakuBtn addTarget:self action:@selector(danmakuShow:) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - Action
- (void)backClick {
    
    // player加到控制器上，只有一个player时候
    // 状态条的方向旋转的方向,来判断当前屏幕的方向
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    // 是竖屏时候响应关
    if (orientation == UIInterfaceOrientationPortrait &&
        (self.playerView.state == StatePlaying)) {
        
        /** 关闭时以小窗口播放模式
         [SuperPlayerWindowShared setSuperPlayer:self.playerView];
         [SuperPlayerWindowShared show];
         SuperPlayerWindowShared.backController = self;
         */
    } else {
        [self.playerView resetPlayer];  //非常重要
        SuperPlayerWindowShared.backController = nil;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didMoveToParentViewController:(UIViewController *)parent {
    // 注销时释放内存空间
    if (parent == nil) {
        if (!SuperPlayerWindowShared.isShowing) {
            [self.playerView resetPlayer];
        }
    }
}



#pragma mark - DSVideoShareViewDelegate

- (void)videoShareViewDidContinuePlayer:(UIButton *)sender {
    [self.shareView removeFromSuperview];
}



#pragma mark - SuperPlayerDelegate
- (void)superPlayerBackAction:(SuperPlayerView *)player {
    [self backClick];
}

/// 全屏改变通知
- (void)superPlayerFullScreenChanged:(SuperPlayerView *)player {
    
}

/// 播放开始通知
- (void)superPlayerDidStart:(SuperPlayerView *)player {
   
    
}

/// 播放结束通知
- (void)superPlayerDidEnd:(SuperPlayerView *)player {
    
    
}
/// 播放错误通知
- (void)superPlayerError:(SuperPlayerView *)player errCode:(int)code errMessage:(NSString *)why {
    
}


- (void)superPlayerProgressTime:(SuperPlayerView *)player currentTime:(NSInteger)currentTime {
    
}

#pragma mark - Lazy

- (UIView *)playerFatherView {
    if (!_playerFatherView) {
        _playerFatherView = [[UIView alloc] init];
        _playerFatherView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _playerFatherView;
}

- (SuperPlayerView *)playerView {
    if (!_playerView) {
        _playerView = [[SuperPlayerView alloc] init];
        _playerView.delegate = self;
        // 添加弹幕视图
        [self setupDanmaku];
    }
    return _playerView;
}

- (DSVideoShareView *)shareView {
    if (!_shareView) {
        _shareView = [[DSVideoShareView alloc] init];
        _shareView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        _shareView.delegate = self;
    }
    return _shareView;
}


@end

